<?php

namespace App\Http\Middleware;

use Closure;

class AuthSignMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $array_uid = [];
        preg_match('#M[0-9]{13}#',$request->path(),$array_uid);
        $uid = $array_uid[0];
        if(isset($_SERVER['HTTP_SIGN'])){
            $sign = $_SERVER['HTTP_SIGN'];
        }else{
            $sign = '';
        }
        if(md5(app('db')->table('app_user_token')->where('uid','=',$uid)->value('token').env('USER_TOKEN_KEY').$uid)==strtolower($sign)){
            return $next($request);
        }else{
            return response()->json(['status_code'=>AUTH_SIGN_ERROR,'msg'=>'用户验证失败']);
        }

    }

}
